//
//  Emoticons.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/18.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit


class Emotion: BaseModel {

    var emoji : String?

    /// emoji的编码
    var code : String?
    {
        didSet
        {
            guard let code = code else {
                return
            }
            
            var val : UInt32 = 0
            Scanner(string: code).scanHexInt32(&val)
            emoji = String(Character(UnicodeScalar(val)!))
        }
    }
    
    /// 分组
    var type : String?
    /// 对应中文编码
    var chs : String?
    
    var img : UIImage?
    
    var dicectory : String?

    /// 对应图片名
    var png : String?
    {
        didSet
        {
            guard let png = png else {
                return
            }
            guard let dicectory = dicectory else {
                return
            }
            guard let path = Emoticons.path(filename: png, directory: dicectory) else {
                return
            }
            img = UIImage(contentsOfFile: path)
        }
    }

}


class Emoticons : NSObject {
    
    var data : Array<Array<Emotion>?>?
    
    
    var emoji : Array<Emotion>?
    var defaut : Array<Emotion>?
    var lxh : Array<Emotion>?
    
    static var bundelPath : String! {
        return Bundle.main.path(forResource:"Emoticons", ofType: "bundle")
    }
    static func path(filename: String?, directory: String?) -> String? {
        guard let bundelPath = Emoticons.bundelPath  else {
            HHLog("Emoticons.bundle文件异常")
            return nil
        }
        return Bundle(path: bundelPath)?.path(forResource: filename, ofType:nil, inDirectory: directory)
    }
    
    
    override init() {
        super.init()
        
        
        emoji = setupData(dicectory: "com.apple.emoji")
        defaut = setupData(dicectory: "com.sina.default")
        lxh = setupData(dicectory: "com.sina.lxh")
        
        data = Array()
        data?.append(emoji)
        data?.append(defaut)
        data?.append(lxh)

        HHLog(emoji?.first?.code)
        HHLog(emoji?.first?.emoji)

        
        
    }
    
    
    func setupData(dicectory: String) -> Array<Emotion>? {
        
        guard let plistPath = Emoticons.path(filename: "info.plist", directory: dicectory) else {
            HHLog("info.plist文件异常")
            return nil
        }
        
        guard let plist = NSArray(contentsOfFile: plistPath) else {
            return nil
        }
        
        var list = Array<Emotion>()
        for keyvalues in plist {
            var dict = (keyvalues as! Dictionary<String, Any>)
            dict["dicectory"] = dicectory
            list.append(Emotion(keyvalues: dict))
        }
        return list
    }

    
}
